package ac.beiwo.baas.admin.controller;

import ac.beiwo.baas.admin.entity.Clazz;
import ac.beiwo.baas.admin.entity.Field;
import ac.beiwo.baas.admin.entity.FieldType;
import ac.beiwo.baas.admin.service.ClazzService;
import ac.beiwo.baas.admin.service.FieldService;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.common.entity.SimpleResult;
import ac.beiwo.baas.file.service.FileService;
import ac.beiwo.baas.user.service.InstallationService;
import ac.beiwo.baas.user.service.UserService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 完成初始化操作
 * <p/>
 * Created by Staryet on 15/8/13.
 */
@RestController
@RequestMapping(value = "/admin/init")
public class InitController {

    @Autowired
    private ClazzService clazzService;
    @Autowired
    private FieldService fieldService;

    /**
     * 初始化内部类
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public SimpleResult initClass() throws SimpleError {
        //初始化用户类
        try {
            clazzService.delete(UserService.USER_CLASS_NAME, false);
        } catch (SimpleError ignored) {
        }
        Clazz clazzUser = new Clazz();
        clazzUser.setName(UserService.USER_CLASS_NAME);
        clazzUser.setInternal(true);
        clazzService.insert(clazzUser, false);
        //初始化用户类字段
        Field username = new Field(FieldType.STRING, "username");
        username.setInternal(true);
        fieldService.insert(UserService.USER_CLASS_NAME, username);
        Field password = new Field(FieldType.STRING, "password");
        password.setInternal(true);
        fieldService.insert(UserService.USER_CLASS_NAME, password);
        Field email = new Field(FieldType.STRING, "email");
        email.setInternal(true);
        fieldService.insert(UserService.USER_CLASS_NAME, email);
        Field phone = new Field(FieldType.STRING, "phone");
        phone.setInternal(true);
        fieldService.insert(UserService.USER_CLASS_NAME, phone);
        Field sessionToken = new Field(FieldType.STRING, "sessionToken");
        sessionToken.setInternal(true);
        fieldService.insert(UserService.USER_CLASS_NAME, sessionToken);

        //初始化设备类
        try {
            clazzService.delete(InstallationService.INSTALLATION_CLASS_NAME, false);
        } catch (SimpleError ignored) {
        }
        Clazz installationClazz = new Clazz();
        installationClazz.setName(InstallationService.INSTALLATION_CLASS_NAME);
        installationClazz.setInternal(true);
        clazzService.insert(installationClazz, false);
        //初始化设备类字段
        Field installationId = new Field(FieldType.STRING, "installationId");
        installationId.setInternal(true);
        fieldService.insert(InstallationService.INSTALLATION_CLASS_NAME, installationId);
        Field deviceToken = new Field(FieldType.STRING, "deviceToken");
        deviceToken.setInternal(true);
        fieldService.insert(InstallationService.INSTALLATION_CLASS_NAME, deviceToken);
        Field deviceType = new Field(FieldType.STRING, "deviceType");
        deviceType.setInternal(true);
        fieldService.insert(InstallationService.INSTALLATION_CLASS_NAME, deviceType);

        //初始化文件类
        try {
            clazzService.delete(FileService.FILE_CLASS_NAME, false);
        } catch (SimpleError ignored) {
        }
        //初始化文件字段
        Clazz fileClazz = new Clazz();
        fileClazz.setName(FileService.FILE_CLASS_NAME);
        fileClazz.setInternal(true);
        clazzService.insert(fileClazz, false);
        //初始化文件类字段
        Field nameField = new Field(FieldType.STRING, "name");
        nameField.setInternal(true);
        fieldService.insert(FileService.FILE_CLASS_NAME, nameField);
        Field urlField = new Field(FieldType.STRING, "url");
        urlField.setInternal(true);
        fieldService.insert(FileService.FILE_CLASS_NAME, urlField);
        Field keyField = new Field(FieldType.STRING, "key");
        keyField.setInternal(true);
        fieldService.insert(FileService.FILE_CLASS_NAME, keyField);

        return SimpleResult.success();
    }

}
